package com.qingpeng.pay.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.qingpeng.pay.R;
import com.qingpeng.pay.api.Host;
import com.qingpeng.pay.api.RedBag;
import com.qingpeng.pay.base.BaseActivity;
import com.qingpeng.pay.beans.NullBean;
import com.qingpeng.pay.okhttp.RetrofitCreateHelper;
import com.qingpeng.pay.utils.StringUtils;
import com.qingpeng.pay.utils.ToastUtils;
import com.qingpeng.pay.views.TitleBar;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class ForgetPwdActivity extends BaseActivity<NullBean> {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_login)
    Button btnLogin;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget_pwd;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ForgetPwdActivity.this.finish();
            }
        });
    }

    @OnClick(R.id.btn_login)
    public void onViewClicked() {
        String pwd = etPhone.getText().toString();
        if (StringUtils.isEmpty(pwd)) {
            ToastUtils.showToast(this,"请输入密码");
            return;
        }
        if (pwd.length() < 6 || pwd.length() > 20) {
            ToastUtils.showToast(this,"密码长度6-20位");
            return;
        }
        String pwdAgain = etCode.getText().toString();
        if (StringUtils.isEmpty(pwdAgain)) {
            ToastUtils.showToast(this,"请输入密码");
            return;
        }
        if (pwdAgain.length() < 6 || pwdAgain.length() > 20) {
            ToastUtils.showToast(this,"密码长度6-20位");
            return;
        }

        if (!pwd.equals(pwdAgain)){
            ToastUtils.showToast(this,"两次密码不一样");
        }
        call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).setpwd(pwd);
        call.enqueue(new Callback<NullBean>() {
            @Override
            public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                NullBean body = response.body();
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(ForgetPwdActivity.this,body.getInfo());
                    return;
                }
                ToastUtils.showToast(ForgetPwdActivity.this,body.getInfo());
                ForgetPwdActivity.this.finish();
            }

            @Override
            public void onFailure(Call<NullBean> call, Throwable t) {
                ToastUtils.showToast(ForgetPwdActivity.this,"网络请求错误,请稍后重试");
            }
        });
    }
}
